package com.njzxw.fs.commonutil.util;

import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Service
public class CookieUtil {

    /**
     * 根据名字获取cookie
     *
     * @param request
     * @param name cookie名字
     * @return
     */
    public String getCookieByName(HttpServletRequest request, String name) {
        Map<String, Cookie> cookieMap = readCookieMap(request);
        if (cookieMap.containsKey(name.concat(getBusiness(request)))) {
            Cookie cookie = (Cookie) cookieMap.get(name.concat(getBusiness(request)));
            return cookie.getValue();
        } else {
            return "";
        }
    }

    /**
     * 获取 business
     *
     * @param request
     * @return
     */
    public String getBusiness(HttpServletRequest request) {
        String business = request.getParameter("business");
        if (business == null) {
            return "";
        } else {
            return business;
        }
    }

    /**
     * 将cookie封装到Map里面
     *
     * @param request
     * @return
     */
    public Map<String, Cookie> readCookieMap(HttpServletRequest request) {
        Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                cookieMap.put(cookie.getName(), cookie);
            }
        }
        return cookieMap;
    }

    /**
     * 设置cookie
     *
     * @param response
     * @param name cookie名字
     * @param value cookie值
     * @param maxAge cookie生命周期 以秒为单位
     */
    public void addCookie(HttpServletRequest request,HttpServletResponse response, String name,
            String value, int maxAge) {
        addCookie(request,response, name, value, maxAge, "/");
    }

    /**
     * 设置cookie
     *
     * @param response
     * @param name cookie名字
     * @param value cookie值
     */
    public void addCookie(HttpServletRequest request,HttpServletResponse response, String name,
            String value) {
        addCookie(request,response, name, value, 60 * 60 * 24, "/");
    }

    public void addCookie(HttpServletRequest request,HttpServletResponse response, String name,
            String value, int maxAge, String path) {
        String business = getBusiness(request);
        if(business == null || "".equals(business)){
            return;
        }
        Cookie cookie = new Cookie(name.concat(getBusiness(request)), value);
        cookie.setPath(path);
        if (maxAge > 0) {
            cookie.setMaxAge(maxAge);
        }
        response.addCookie(cookie);
    }

    /**
     * 删除cookies
     *
     * @param request
     * @param response
     * @param name
     */
    public void cleanCookie(HttpServletRequest request,
            HttpServletResponse response, String name) {
        cleanCookie(request, response, name, "/");
    }

    /**
     * 删除cookies
     *
     * @param request
     * @param response
     * @param name
     * @param path 必须与设置的一样
     */
    public void cleanCookie(HttpServletRequest request,
            HttpServletResponse response, String name, String path) {
        Cookie cookies[] = request.getCookies();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals(name.concat(getBusiness(request)))) {
                    Cookie cookie = new Cookie(name.concat(getBusiness(request)), "");// 这边得用"",不能用null
                    cookie.setPath(path);// 设置成跟写入cookies一样的
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }
            }
        }
    }

}
